package com.bltec.emergency.controller.resController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ResHelicopter;
import com.bltec.emergency.service.resService.ResHelicopterService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/resHelicopter")
@RequiresPermissions("project:res")
public class ResHelicopterController {
    @Resource
    private ResHelicopterService resHelicopterService;
    @Resource
    private UserService userService;
    /**
     * 获得所有resHelicopter
     */
    @RequestMapping("getAllResHelicopter")
    public ResponseJson getResHelicopter() {
        try {
            List<ResHelicopter> resHelicopter = resHelicopterService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(resHelicopter));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加resHelicopter
     */
    @RequiresPermissions("res:insert")
    @RequestMapping("addResHelicopter")
    public ResponseJson addResHelicopter(String model, String company, Date productionTime, String base, String manufacturer, String registrationNum) {
        try {
            ResHelicopter resHelicopter = new ResHelicopter();
            resHelicopter.setModel(model);
            resHelicopter.setCompany(company);
            resHelicopter.setProductionTime(productionTime);
            resHelicopter.setBase(base);
            resHelicopter.setManufacturer(manufacturer);
            resHelicopter.setRegistrationNum(registrationNum);
            resHelicopter.setEnable(true);
            resHelicopter.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resHelicopter.setCreateTime(DateUtil.dateNow());
            resHelicopterService.add(resHelicopter);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    /**
     * 修改resHelicopter
     */
    @RequiresPermissions("res:update")
    @RequestMapping("updateResHelicopter")
    public ResponseJson updateResHelicopter(Long id, String model, String company, Date productionTime, String base, String manufacturer, String registrationNum) {
        try {
            ResHelicopter resHelicopter = resHelicopterService.getResHelicopterById(id);
            resHelicopter.setModel(model);
            resHelicopter.setCompany(company);
            resHelicopter.setProductionTime(productionTime);
            resHelicopter.setBase(base);
            resHelicopter.setManufacturer(manufacturer);
            resHelicopter.setRegistrationNum(registrationNum);
            resHelicopter.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resHelicopter.setUpdateTime(DateUtil.dateNow());
            resHelicopterService.updateResHelicopterById(resHelicopter);
            return new ResponseJson(Constant.OK, JSON.toJSON(resHelicopter));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    /**
     * 删除resHelicopter
     */
    @RequiresPermissions("res:delete")
    @RequestMapping("deleteResHelicopter")
    public ResponseJson setDisable(Long id) {
        if (null != resHelicopterService.getResHelicopterById(id)) {
            if (resHelicopterService.deleteResHelicopterById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
